<?php
/**
 * @file articlerightdata.php
 * @author LinLin
 * @date 2016年5月19日 下午12:27:07
 * @brief 
 */
namespace haibao\cms\business\control\pc\channel\article;

use \haibao\cms\model\filter\Article as ArticleFilter;
use haibao\cms\model\filter\ArticleTag as ArticleTagFilter;
use haibao\cms\model\filter\ArticleProImage as ArticleProImageFilter;
use haibao\cms\model\filter\ArticleProperty as ArticlePropertyFilter;
use haibao\cms\common\ArticleContent as ArticleContentClass;
use \haibao\frame\data\query\Condition;
use haibao\frame\http\UserControl;
use \haibao\cms\business\Article as ArticleBusiness;


class ArticleRightDataController extends \haibao\cms\business\control\pc\channel\ChannelController{

    public function __construct($updateTime = null,$controlId,$isPublish){
        parent::__construct($updateTime,$controlId,$isPublish);
    }
    
    /**
     * 显示模版内容（模版动态数据的绑定和模版输出）
     * @param : String $content
     * @param : Array $controlData;
     */
    public function displayTemplateHtml(){
        $controlData = $this->getParameter(self::$CONTROL_DATA);
        $content = $this->getParameter(self::$CONTROL_CONTENT);
        $path = $this->generateHtmlPath(__CLASS__);
        
       $channelTag = array(
           'www'=>6,'stars'=>1383,'fashion'=>310,'beauty'=>24,'life'=>520,'brands'=>955,'accessory'=>7455,'jewelrywatch'=>6863,'wedding'=>211,'tv'=>11280
       );
  
       $feature = $exclusiveSerial = $exclusive = $exclusiveInfo = array();
       $exclusive = \haibao\cms\business\control\FeatureController::getFeatureSerialArray();
       if($exclusive){
           foreach($exclusive as $key=>$exclusiveVal){
               if(isset($exclusiveVal['children']) && !empty($exclusiveVal['children'])){
                   foreach($exclusiveVal['children'] as $k=>$val){
                       $feature[$exclusiveVal['Id']][] = $val['Id'];
                   }
               }
           }
       }
       if($feature){
           foreach($feature as $typeId=>$serialId){
               if($typeId == 47){
                   $exclusiveSerial['stars']['exclusive'] = $feature[$typeId];
                   $exclusiveSerial['stars']['column'] = array();
               }
               if($typeId == 48){
                   $exclusiveSerial['fashion']['exclusive'] = $feature[$typeId];
                   $exclusiveSerial['fashion']['column'] = array(12204);
               }
               if($typeId == 49){
                   $exclusiveSerial['beauty']['exclusive'] = $feature[$typeId];
                   $exclusiveSerial['beauty']['column'] = array();
               }
           }
       }
       
       //独家策划
        $exclusiveInfo['stars'] = $this->getExclusive($exclusiveSerial['stars']['exclusive'],$exclusiveSerial['stars']['column']);
        $exclusiveInfo['fashion']  = $exclusiveInfo['brands'] = $this->getExclusive($exclusiveSerial['fashion']['exclusive'],$exclusiveSerial['fashion']['column']);
        $exclusiveInfo['beauty'] = $this->getExclusive($exclusiveSerial['beauty']['exclusive'],$exclusiveSerial['beauty']['column']);
        $exclusiveInfo['life'] = $exclusiveInfo['diy'] = $exclusiveInfo['wedding'] = $exclusiveInfo['beauty'];
        $exclusiveInfo['www'] =  $exclusiveInfo['accessory'] = $exclusiveInfo['jewelrywatch'] = $exclusiveInfo['stars'];
        $exclusive = array();
        if($exclusiveInfo){
            foreach($exclusiveInfo as $key=>$val){
                $fans = array();
                foreach ($val as $info) {
                    $fans[] = $info['ListTime'];
                }
                array_multisort($fans, SORT_DESC, $val);
                $exclusive[$key] = $val;
            }
        }

        $result['exclusive'] = $exclusive;
        
        //猜你喜欢
        $temp['stars'] = $this->getGuessLikeData($channelTag['stars']);
        $temp['fashion'] = $temp['www'] = $this->getGuessLikeData($channelTag['fashion']);
        $temp['beauty'] = $this->getGuessLikeData($channelTag['beauty']);
        $temp['diy'] = $temp['life'] = $this->getGuessLikeData($channelTag['life']);
        $temp['brands'] = $this->getGuessLikeData($channelTag['brands']);
        $temp['accessory'] = $this->getGuessLikeData($channelTag['accessory']);
        $temp['jewelrywatch'] = $this->getGuessLikeData($channelTag['jewelrywatch']);
        $temp['wedding'] = $this->getGuessLikeData($channelTag['wedding']);
        $temp['tv'] = $this->getGuessLikeData($channelTag['tv']);
        $result['guess'] = $temp;
        
        $this->assign('articleRightData',json_encode($result));

        
        $parameters = $this->getControlParameters($controlData);
        $this->assign('parameters', $parameters);
        //$this->assign('userHostColler', \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_USER));
        $this->generateHtml($path, $content);
        return $this->getHtml($path);
    }
    
    private function getExclusive($serialIds,$columIds){
        $exclusive = $columList = $column = $featureList = $featureListAll = array();
        if($serialIds){
            foreach($serialIds as $serialId){
                $featureList = \haibao\cms\business\control\FeatureController::getserialInfoById($serialId);
                if($featureList){
                    $featureListAll[] = $featureList[0];
                }
            }
        }
       
       
        if($featureListAll){
            foreach($featureListAll as $key=>$val){
                $exclu['FeatureName'] = $val->FeatureDefine->FeatureName;
                $exclu['Url'] = $val->Url;
                $exclu['ImageUrl'] = $this->getImageLink($val->Image->Filename,300,225,100,0);
                $exclu['ListTime'] = $val->ListTime->format('Y-m-d H:i:s');
                $exclusive[] = $exclu;
            }
        }
        if(count($exclusive) < 2){
            $dataCondition['limit'] = 2-count($exclusive);
            $dataCondition['tagId'] = implode(',',$columIds);
            $dataCondition['isVote'] = 1;
            $dataCondition['proLevel'] = 5;
            $this->parseTagId($dataCondition['tagId']);
            $columList = $this->getActualData($dataCondition);
            if($columList){
                foreach($columList as $key=>$val){
                    $col['FeatureName'] = $val['Article']->Title;
                    $col['Url'] = $val['ChannelUrl'];
                    $col['ImageUrl'] = $this->getImageLink($val['Article']->ArticleProImage,300,225,100,0);
                    $column[] = $col;
                }
            }
        }
        return  array_merge($exclusive,$column);
    }
    
    public function getActualData($dataCondition){
        if(!isset($dataCondition['level'])) $dataCondition['level'] = 1;
        $dataCondition['isProImage'] = true;
        $dataCondition['isParentTag'] = false;
        $dataCondition['isTag'] = true;
        $dataCondition['isConditionTag'] = true;
        $dataCondition['isPrimaryTag'] = true;
        $dataCondition['isReturnTag'] = true;
        $dataCondition['isImageColl'] = false;
        $dataCondition['source'] = 'fashionTodayNew';
        $proLevel =$dataCondition['proLevel'];
        $dataCondition['proImageLevel'] = array(\haibao\cms\model\data\ArticleProImage::Level_ZERO,$dataCondition['proLevel']);
        list($articles,$articleProImages,$tagInfo) = $this->getArticleControl(1,$dataCondition);
        $articleList = array();
        $articleLists = array();
        $this->tagInfo = $tagInfo;
        foreach($articles as $key=>$article){
            $articleList['TagInfo'] = $tagInfo[$article->ArticleTag->TagId];
            $articleList['ChannelUrl'] = self::getArticleUrl($article->PublishId,$article->ArticleTag->ParentId);
            if(empty($article->ArticleProImage)){
                if(isset($articleProImages[$article->Id][$proLevel])){
                    $article->ArticleProImage = $articleProImages[$article->Id][$proLevel];
                }else{
                    if(isset($articleProImages[$article->Id][\haibao\cms\model\data\ArticleProImage::Level_FIVE])){
                        $article->ArticleProImage = $articleProImages[$article->Id][\haibao\cms\model\data\ArticleProImage::Level_FIVE];
                    }else{
                        if(isset($articleProImages[$article->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO])){
                            $article->ArticleProImage = $articleProImages[$article->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO];
                        } else {
                            $article->ArticleProImage = \haibao\cms\business\control\PublicController::getArticleFirstImage($article->Id);
                        }
                    }
                }
            }
            $articleList['Article'] = $article;
            array_push($articleLists,$articleList);
        }

        
        //$articleList =  json_decode(json_encode($articleList));
        return $articleLists;
    }
    
    private function getGuessLikeData($topTagId){
        $articleList = array();
        $articleFilter = new ArticleFilter();
        $articlePropertyFilter = new ArticlePropertyFilter();
        $articleFilter->select(
            array(
                ArticleFilter::CONDITION_FIELD_NAME_ID,
                ArticleFilter::CONDITION_FIELD_NAME_TITLE,
                ArticleFilter::CONDITION_FIELD_NAME_SHORTTITLE,
                ArticleFilter::CONDITION_FIELD_NAME_PUBLISHTIME,
                ArticleFilter::CONDITION_FIELD_NAME_ARTICLE_PUBLISHID,
            )
        );
         
        $articleFilter->where(ArticleFilter::CONDITION_FIELD_NAME_PUBLISHTIME,Condition::CONDITION_BETWEEN,array(date('Y-m-d',strtotime('-1 week')),date('Y-m-d')));
        $articleFilter->where(ArticleFilter::CONDITION_FIELD_NAME_LEVEL,Condition::CONDITION_GREATEREQUAL,5);
        
        //$articleFilter->where(ArticleFilter::CONDITION_FIELD_NAME_ID,Condition::CONDITION_NOTEQUAL,$articleId);
         
        $articlePropertyFilter->select(
            array(
                ArticlePropertyFilter::CONDITION_FIELD_NAME_ARTICLE_ID,
                ArticlePropertyFilter::CONDITION_FIELD_NAME_ARTICLE_EDIT_CLICK_NUM,
            )
        );
         
        $articleFilter->limit(4);
        $articleFilter->order(ArticlePropertyFilter::CONDITION_FIELD_NAME_ARTICLE_EDIT_CLICK_NUM,false,$articlePropertyFilter);
        $articleFilter->order(ArticlePropertyFilter::CONDITION_FIELD_NAME_ARTICLE_COMMENT_NUM,false,$articlePropertyFilter);
         
        $articleFilter->leftJoin($articlePropertyFilter,null,ArticleFilter::CONDITION_FIELD_NAME_ID,ArticlePropertyFilter::CONDITION_FIELD_NAME_ARTICLE_ID,ArticleFilter::CONDITION_FIELD_NAME_ARTICLE_PROPERTY,false);
         
        $articleTagFilter = new ArticleTagFilter();
        $articleTagFilter->select(
            array(
                ArticleTagFilter::CONDITION_FIELD_NAME_ARTICLE_ID,
                ArticleTagFilter::CONDITION_FIELD_NAME_TAG_ID,
                ArticleTagFilter::CONDITION_FIELD_NAME_PARENT_ID,
            )
        );
        $articleTagFilter->where(ArticleTagFilter::CONDITION_FIELD_NAME_PARENT_ID,Condition::CONDITION_EQUAL,$topTagId);
        $articleTagFilter->where(ArticleTagFilter::CONDITION_FIELD_NAME_IS_PRIMAIY,Condition::CONDITION_EQUAL,\haibao\cms\model\data\ArticleTag::PRIMAIY_IS);
        
        $articleProImageFilter = new ArticleProImageFilter();
        $articleProImageFilter->select(array(
            ArticleProImageFilter::CONDITION_FIELD_NAME_ARTICLE_ID,
            ArticleProImageFilter::CONDITION_FIELD_NAME_IMAGE_ID,
            ArticleProImageFilter::CONDITION_FIELD_NAME_PIC_URL
        ));
        $articleProImageFilter->where(ArticleProImageFilter::CONDITION_FIELD_NAME_PIC_TYPE, Condition::CONDITION_EQUAL, \haibao\cms\model\data\ArticleProImage::COVER_LEVEL_ONE);
        $articleProImageFilter->where(ArticleProImageFilter::CONDITION_FIELD_NAME_ID, Condition::CONDITION_OR,array(
            new Condition(ArticleProImageFilter::CONDITION_FIELD_NAME_LEVEL,Condition::CONDITION_EQUAL,4),
            new Condition(ArticleProImageFilter::CONDITION_FIELD_NAME_LEVEL,Condition::CONDITION_EQUAL,0)
        ));
        
        $articleFilter->leftJoin($articleTagFilter,null,ArticleFilter::CONDITION_FIELD_NAME_ID,ArticleTagFilter::CONDITION_FIELD_NAME_ARTICLE_ID,ArticleFilter::CONDITION_FIELD_NAME_ARTICLE_TAG,false);
         
        $articleFilter->leftJoin($articleProImageFilter,null,ArticleFilter::CONDITION_FIELD_NAME_ID,ArticleProImageFilter::CONDITION_FIELD_NAME_ARTICLE_ID,ArticleFilter::CONDITION_FIELD_NAME_ARTICLE_PRO_IMAGE,false);
        
        $articleFilter->where(ArticleFilter::CONDITION_FIELD_NAME_STATUS,Condition::CONDITION_EQUAL,\haibao\cms\model\data\Article::STATUS_ENABLE);
        $articleFilter->where(ArticleFilter::CONDITION_FIELD_NAME_PUBLISHED,Condition::CONDITION_EQUAL,\haibao\cms\model\data\Article::PUBLISHED_YES);
        //$articlePropertyFilter->where(ArticlePropertyFilter::CONDITION_FIELD_NAME_CLICK_NUM,Condition::CONDITION_GREATER,0);
        $list = ArticleBusiness::getArticleList($articleFilter);
        $imageUrls  = $tagInfo = array();

        if($list){
            foreach($list as $key=>$val){
                $imageUrls[$val->ArticleProImage->ImageId] = $val->ArticleProImage->PicUrl;
            }
            foreach($list as $key=>$val){
                if(isset($imageUrls[$val->ArticleProImage->ImageId])){
                    $list[$key]->ArticleProImage->PicUrl = $this->getImageLink($imageUrls[$val->ArticleProImage->ImageId],100,100,100,0);
                }
            }
            foreach($list as $key=>$val){
                $temp['Id'] = $val->Id;
                $temp['title'] = $this->cutTitle($val->Title, 20,true);
                $temp['shortTitle'] = $this->cutTitle($val->ShortTitle, 20,true);
                $temp['originalTitle'] = !empty($val->ShortTitle) ? $val->ShortTitle : $val->Title;
                $temp['ImageUrl'] = $val->ArticleProImage->PicUrl;
                $temp['Url'] = $this->getArticleUrl($val->PublishId,$topTagId);
                $articleList[] = $temp;
            }
        }

        return $articleList;
    }
} 